﻿using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace DevelopersToolbox.Util {

    public class Caret : IDisposable {
        private readonly IWin32Window _parent;

        [DllImport("user32.dll")]
        static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);

        [DllImport("user32.dll")]
        static extern bool DestroyCaret();

        [DllImport("user32.dll")]
        static extern bool ShowCaret(IntPtr hWnd);

        [DllImport("user32.dll")]
        static extern bool SetCaretPos(int x, int y);

        public Caret(IWin32Window parent, int width, int height) {
            _parent = parent;
            CreateCaret(_parent.Handle, IntPtr.Zero, width, height);
            ShowCaret(_parent.Handle);
        }

        public void Dispose() {
            DestroyCaret();
        }

        public void SetPosition(int x, int y) {
            SetCaretPos(x, y);
        }
    }
}